package org.example.service.impl;

import cn.hutool.core.bean.BeanUtil;
import org.example.dao.ISysDepartmentDao;
import org.example.dao.ISysUserDao;
import org.example.domain.entity.SysDepartment;
import org.example.domain.entity.SysUser;
import org.example.domain.vo.DeptVo;
import org.example.domain.vo.SysUserVo;
import org.example.service.ISysDeptService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysDeptServiceImpl implements ISysDeptService {
    private final ISysDepartmentDao sysDepartmentDao;
    private final ISysUserDao userDao;

    public SysDeptServiceImpl(ISysDepartmentDao sysDepartmentDao, ISysUserDao userDao) {
        this.sysDepartmentDao = sysDepartmentDao;
        this.userDao = userDao;
    }

    @Override
    public List<DeptVo> selectAllDepts() {
        List<DeptVo> deptVos = new ArrayList<>();

        List<SysDepartment> departments = sysDepartmentDao.selectList(null);

        for (SysDepartment department : departments) {
            DeptVo deptVo = new DeptVo();
            List<SysUserVo> userVos = new ArrayList<>();
            List<SysUser> users = userDao.selectUserById(department.getId());
            for (SysUser user : users) {
                SysUserVo sysUserVo = new SysUserVo();
                BeanUtil.copyProperties(user,sysUserVo);
                userVos.add(sysUserVo);
            }
            BeanUtil.copyProperties(department,deptVo);

            deptVo.setUserVos(userVos);
            deptVos.add(deptVo);
        }

        return deptVos;
    }

    public void selectDeptById(){

    }
}
